package org.maven.dependencybrowser.implementations.local;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.maven.dependencybrowser.controller.DependencyResolutionException;
import org.maven.dependencybrowser.implementations.Repository;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class LocalDiskRepository implements Repository, Contextualizable
{

    public static final String KEY_LOCAL_REPO_PATH = "local.repo";

    private File repositoryRoot;

    public Model resolveFile(String groupId, String artifactId, String version)
    {
        String[] groups = groupId.split("\\.");
        File artifactPomFile = new File(repositoryRoot.getPath());
        for (int i = 0; i < groups.length; i++)
        {
            artifactPomFile = new File(artifactPomFile, groups[i]);
        }
        artifactPomFile = new File(artifactPomFile, artifactId);
        artifactPomFile = new File(artifactPomFile, version);
        artifactPomFile = new File(artifactPomFile, artifactId + "-" + version + ".pom");

        if (!artifactPomFile.exists())
        {
            return null;
        }

        MavenXpp3Reader pomReader = new MavenXpp3Reader();
        try
        {
            return pomReader.read(new FileReader(artifactPomFile));
        }
        catch (FileNotFoundException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (IOException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (XmlPullParserException e)
        {
            //throw new DependencyResolutionException(e);
        }
        return null;
    }

    public File getRepositoryRoot()
    {
        return this.repositoryRoot;
    }

    public boolean canCreateSnapshot()
    {
        return true;
    }

    public void contextualize(Context context) throws ContextException
    {
        String repoPath = (String) context.get(KEY_LOCAL_REPO_PATH);
        if (repoPath == null || repoPath.trim().length() == 0)
        {
            throw new ContextException("Missing configuration parameter: " + KEY_LOCAL_REPO_PATH);
        }
        repositoryRoot = new File(repoPath);
    }

}
